import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home'
import Welcome from "@/components/Welcome";
import Users from "@/components/user/Users";
import Rights from "@/components/power/Rights";
import Roles from "@/components/power/Roles";
import Cate from "@/components/goods/Cate";
import Params from "@/components/goods/Params";
import List from "@/components/goods/List";
import Add from "@/components/goods/Add";
import Order from "@/components/order/Order";
import Report from "@/components/report/Report";

Vue.use(VueRouter)

const routes = [
  {
    name: 'Login',
    path: '/Login',
    component: Login
  },
  {
    name: 'root',
    path: '/',
    redirect: '/login'
    },
  {
    name: 'home',
    path: "/home",
    component: Home,
    redirect: '/welcome',
    children: [
      {
        name:'welcome',
        path: '/welcome',
        component: Welcome
      },
      {
        name: "users",
        path: "/users",
        component: Users
      },
      {
        name:'rights',
        path: '/rights',
        component: Rights
      },
      {
        name: 'roles',
        path: '/roles',
        component: Roles
      },
      {
        name: 'cate',
        path: '/shopType',
        component: Cate
      },
      {
        name: 'params',
        path: '/params',
        component: Params
      },
      {
        name: 'List',
        path: '/shopList',
        component: List
      },
      {
        name: 'Add',
        path: '/shop/add',
        component: Add
      },
      {
        name: 'Order',
        path: '/orders',
        component: Order
      },
      {
        name: 'Report',
        path: '/report',
        component: Report
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

/**
 * 挂载路由导航导航守卫
 */
router.beforeEach((to, from, next) =>{
  // to 将要访问的地址
  // from
  // next 是一个函数，表示方行；next（） 放行， next（‘/login’）强制跳转

  if (to.path === '/login') return next();
  // 获取token
  const tokenStr = window.sessionStorage.getItem("token");
  if (!tokenStr) return next ("/login");
  next();
});

export default router
